package com.lz.football.module.bag
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.BagEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.type.GoodsType;
	import com.lz.football.module.bag.item.BagItem;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.comp.TabButtonBar;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.LButton;
	import lz.controls.PageComponent;
	import lz.controls.core.TitleWindow;
	import lz.controls.events.ListEvent;
	import lz.controls.events.PageEvent;

	/**
	 *背包 
	 * @author lake
	 * 
	 */	
	public class BagView extends TitleWindow implements IBagView
	{
		/**
		 *第一个格子对象 
		 */		
		public static var firstItem :Sprite;
		/**
		 *关闭按钮 
		 */		
		public static var CLoseBtn	 :SimpleButton;
		/**
		 *选项卡 
		 */		
		private var buttonBar :TabButtonBar;
		/**
		 *容器 
		 */		
		private var container 	:MovieClip;
		/**
		 * 物品item 列表
		 */		
		private var itemList	:Array = [];
		/**
		 *分页 
		 */		
		private var pageComp 	:PageComponent;
		/**
		 *格子数据 
		 */		
		private var cellDatas	:Array = [];
		/**
		 *显示的商品 
		 */		
		private var showGoods  :Array = [];
		/**
		 *选择的物品item  
		 */		
		private var selectItem  :BagItem;
		/**
		 *整理 
		 */		
		private var sortBtn	:SimpleButton;
		/**
		 *使用 
		 */		
		public static var useBtn	:SimpleButton;
		/**
		 *分解 
		 */		
		private var resolveBtn	:SimpleButton;
		/**
		 *是否点击排序 
		 */		
		private var isSort 	:Boolean = false;
		
		public function BagView()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */		
		public function initUI(view :Object = null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.module.bag.PanelSkin");
			this.addChild(_symbolSkin);
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			container = _symbolSkin.container;
			container.x = 15;
			container.y = 107;
			sortBtn  = _symbolSkin.sortBtn;
			useBtn = _symbolSkin.useBtn;
			resolveBtn = _symbolSkin.resolveBtn;
			//分页初始化
			pageComp = new PageComponent();
			var pageCompSkin :MovieClip = Reflection.createInstance("com.liangzhong.ui.pageComp");
			pageComp.initUI(pageCompSkin);
			_symbolSkin.addChild(pageCompSkin);
			pageCompSkin.x = 260;
			pageCompSkin.y = 349;
			//设置可拖动
			dragEnabled = true;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			//设置坐标不自适应
			resizeEnabled = false;
			//选项bar 初始化
			buttonBar = new TabButtonBar();
			var i :int = 0;
			var lbtn :LButton;
			var lbtns :Array = [];
			for(;i<=5;i++)
			{
				lbtn = new LButton(_symbolSkin["buttonBar"]["skin_" + i]);
				lbtn.enable = true;
				lbtns.push(lbtn);
			}
			buttonBar.setButtons(lbtns);
			buttonBar.selectTabButton(lbtns[0]);
			//添加事件
			addEvtListener();
			//默认显示全部
			showGoods = AppData.getInstance().bagDataManager.goodsList;
			//初始化格子
			var bagItem :BagItem;
			var totalNum :int = AppData.getInstance().bagDataManager.bagInfo.totalCellNum;
			var openNum	 :int = AppData.getInstance().bagDataManager.bagInfo.cashCellNum + AppData.getInstance().bagDataManager.bagInfo.defaultCellNum;
			for(i=0;i<40;i++)
			{
				bagItem = new BagItem();
				bagItem.initUI();
				bagItem.isLock = false;
				bagItem.x = int(i % 10) * 58;
				bagItem.y = int(i / 10) * 59;
				container.addChild(bagItem);
				itemList.push(bagItem);
			}
			//初始化所有格子
			var itemObj :Object;
			for(i = 0;i < totalNum;i++)
			{
				itemObj = {};
				if(i < openNum)
				{
					itemObj.isLock = false;
				}
				else
				{
					itemObj.isLock = true;
				}
				cellDatas.push(itemObj);
			}
			//分页设置
			pageComp.totalNumbers = cellDatas.length;
			pageComp.pageSize = 40;
			pageComp.curPage = 1;
			//显示基本信息
			showBaseInfo();
			//浅入
			fadeIn();
			//guide
			firstItem = itemList[0];
			CLoseBtn = closeBtn;
		}
		/**
		 *显示仓库信息
		 * 
		 */		
		private function showBaseInfo():void
		{
			
		}
		/**
		 *数据重新设置
		 * 升级 开格后
		 */		
		private function resetData():void
		{
			var totalNum :int = AppData.getInstance().bagDataManager.bagInfo.totalCellNum;
			var openNum	 :int = AppData.getInstance().bagDataManager.bagInfo.cashCellNum + AppData.getInstance().bagDataManager.bagInfo.defaultCellNum;
			var cellLen :int = cellDatas.length;
			var itemObj :Object;
			var i :int = 0;
			//如果开多一页 补回显示格子数据
			for(;i < (totalNum - cellLen);i++)
			{
				itemObj = {};
				itemObj.isLock = false;
				cellDatas.push(itemObj);
			}
			//重新设置格子数据
			for(i = 0;i < totalNum;i++)
			{
				itemObj = cellDatas[i];
				if(i < openNum)
				{
					itemObj.isLock = false;
				}
				else
				{
					itemObj.isLock = true;
				}
			}
			//刷新分页组件
			pageComp.totalNumbers = cellDatas.length;
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *页数改变 
		 * @param e
		 * 
		 */		
		private function pageChangeHandler(e :PageEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			//显示格子数据
			setItemPageDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,cellDatas));
			//显示对应页物品
			setBagGoodsDatas(ArrayUtil.getPageSizeDatas(pageComp.curPage,pageComp.pageSize,showGoods));
		}
		/**
		 *设置数据 
		 * @param datas
		 * 
		 */		
		public function setItemPageDatas(datas :Array):void
		{
			var bagItem :BagItem; 
			var i :int = 0;
			for each(var itemObj :* in datas)
			{
				if(i>=40)return;
				bagItem = itemList[i];
				bagItem.isLock = itemObj.isLock;
				i ++;
			}
		}
		/**
		 *设置背包物品数据 
		 * @param datas
		 * 
		 */		
		public function setBagGoodsDatas(datas :Array):void
		{
			var bagItem :BagItem; 
			var i :int = 0;
			for each(var itemObj :* in datas)
			{
				if(i>=40)return;
				bagItem = itemList[i];
				bagItem.data = itemObj;
				i ++;
			}
			for(;i < 40;i++)
			{
				bagItem = itemList[i];
				bagItem.data = null;
			}
		}
		/**
		 *注册事件 
		 * 
		 */		
		public function addEvtListener():void
		{
			pageComp.addEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			this.addEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
			buttonBar.addEventListener(Event.CHANGE,onTabChangeHandler);
			sortBtn.addEventListener(MouseEvent.CLICK,sortClickHandler);
			useBtn.addEventListener(MouseEvent.CLICK,useItemHandler);
			resolveBtn.addEventListener(MouseEvent.CLICK,resoveItemHandler);
			AppData.getInstance().bagDataManager.addEventListener(Event.CHANGE,dataChangeHandler);
			
			DisplayMouseEventManager.instance.addTipsDisplay(resolveBtn,"分解道具产生欧元！\n分解球衣有几率额外获得球衣碎片！\n分解球员有几率额外获得球魂！");
		}
		/**
		 *选项卡改变 
		 * @param e
		 * 
		 */		
		private function onTabChangeHandler(e :Event):void
		{
			refreshData();
			pageComp.curPage = 1;
		}
		/**
		 *刷新数据 
		 * 
		 */		
		private function refreshData():void
		{
			var selectType :int = buttonBar.selectType;
			if(selectType == 2)
			{
				selectType = GoodsType.EQUIP;
			}
			else if(selectType > 2)
			{
				selectType ++;
			}
			showGoods = AppData.getInstance().bagDataManager.getGoodsListByType(selectType);
			if(isSort)
			{
				showGoods.sortOn(["ntype","color"],[Array.NUMERIC,Array.NUMERIC | Array.DESCENDING]);
			}
			pageComp.curPage = pageComp.curPage;
		}
		/**
		 *物品选中 
		 * @param e
		 * 
		 */		
		private function itemClickHandler(e :ListEvent):void
		{
			if(selectItem)
			{
				selectItem.unSelect();
				selectItem = null;
			}
			selectItem = e.target as BagItem;
			selectItem.select();
			//@lake 2013-11-15
			AppEventDispatcher.getInstance().dispatchEvent(new Event("Bag_Guide_Select_Item"));
		}
		/**
		 *点击升级 
		 * @param e
		 * 
		 */		
		private function gradeClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new BagEvent(BagEvent.GRADE_UP));
		}
		/**
		 *整理排序 
		 * @param e
		 */		
		private function sortClickHandler(e :MouseEvent):void
		{
			showGoods.sortOn(["ntype","color"],[Array.NUMERIC,Array.NUMERIC | Array.DESCENDING]);
			pageComp.curPage = pageComp.curPage;
			isSort = true;
		}
		/**
		 *背包数据改变 
		 * @param e
		 * 
		 */		
		private function dataChangeHandler(e :Event):void
		{
			showBaseInfo();
			resetData();
			refreshData();
		}
		/**
		 *使用物品 
		 * @param e
		 * 
		 */		
		private function useItemHandler(e :MouseEvent):void
		{
			if(selectItem == null)
			{
				Alert.showErrorTip("请选择使用的物品");
				return;
			}
			AppEventDispatcher.getInstance().dispatchEvent(new BagEvent(BagEvent.USE_ITEM,selectItem.data));
		}
		/**
		 *分解物品 
		 * @param e
		 * 
		 */		
		private function resoveItemHandler(e :MouseEvent):void
		{
			if(selectItem == null)
			{
				Alert.showErrorTip("请选择分解的物品");
				return;
			}
			var goodsVo :GoodsVo = selectItem.data;
			if(goodsVo.color >= 4)
			{
				Alert.show("该物品为贵重物品，确认分解?","",Alert.YES | Alert.NO,GlobalDef.AlertContainer,resoveAlertH);
			}
			else
			{
				this.dispatchEvent(new BagEvent(BagEvent.RESOLVE_ITEM,selectItem.data,true));
			}
		}
		/**
		 *分解确认 
		 * @param type
		 * 
		 */		
		private function resoveAlertH(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new BagEvent(BagEvent.RESOLVE_ITEM,selectItem.data,true));
			}
		}
		/**
		 *删除事件 
		 * 
		 */		
		public function removeEvtListener():void
		{
			pageComp.removeEventListener(PageEvent.PAGE_CHANGE,pageChangeHandler);
			this.removeEventListener(ListEvent.ITEM_CLICK,itemClickHandler);
			buttonBar.removeEventListener(Event.CHANGE,onTabChangeHandler);
			sortBtn.removeEventListener(MouseEvent.CLICK,sortClickHandler);
			useBtn.removeEventListener(MouseEvent.CLICK,useItemHandler);
			resolveBtn.removeEventListener(MouseEvent.CLICK,resoveItemHandler);
			AppData.getInstance().bagDataManager.removeEventListener(Event.CHANGE,dataChangeHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(resolveBtn);
		}
		/**
		 *舞台大小改变 
		 * @param e
		 * 
		 */		
		override protected function onResizeHandler(e:Event):void
		{
			this.x = (GlobalDef.GameStage.stageWidth - width)/2;
			this.y = (GlobalDef.GameStage.stageHeight - height)/2;
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			_symbolSkin.mouseEnabled = false;
			_symbolSkin.mouseChildren = false;
			fadeOut();
		}
		/**
		 *淡出完毕 
		 */		
		override protected function fadeOutComplete():void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		/**
		 *升级 
		 */		
		public function gradeUp():void
		{
			showBaseInfo();
			resetData();
		}
		/**
		 *开格子 
		 * 
		 */		
		public function openCells():void
		{
			resetData();
		}
		/**
		 *去除时间 
		 * 
		 */		
		public function dropTime():void
		{
			showBaseInfo();
		}
		/**
		 *销毁 
		 * 
		 */		
		public function destroyView():void
		{
			removeEvtListener();
			buttonBar.destroyView();
			buttonBar = null;
			pageComp = null;
			showGoods = null;
			for each(var item :BagItem in itemList)
			{
				item.destroyView();
			}
			itemList = null;
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			firstItem = null;
			CLoseBtn = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 * 
		 */
		public function showType($obj:Object):void
		{
			
		}
	}
}